/**
 * 
 */
package com.hmjk.health.views;



import com.hmjk.health.R;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @author Administrator
 *
 */
public class MutilModeImageTextView extends TextView {

	static final String  TAG="MutilModeImageButton";
    static final int BTN_SELECT_NONE    = 0;
    static final int BTN_SELECT_ONE     = 1;
    static final int BTN_SELECT_TWO     = 2;
    static final int BTN_SELECT_THREE   = 3;
//    static final int BTN_SELECT_FOUR    = 4;
//    static final int BTN_SELECT_FIVE    = 5;
    
    private int mBTN_Secect_state       = 0;

    public int mBTN_ResID_Secect_None       = 0;
    public int mBTN_ResID_Secect_One       = 0;
    public int mBTN_ResID_Secect_Two       = 0;
    public int mBTN_ResID_Secect_Three       = 0;    
//    public int mBTN_ResID_Secect_Four      = 0; 
//    public int mBTN_ResID_Secect_Five      = 0; 
  
    //static 
    private  Bitmap mNoneBmap;
    private  Bitmap mOneBmap;
    private  Bitmap mTwoBmap;
    private  Bitmap mThreeBmap;    
//    private  Bitmap mFourBmap;    
//    private  Bitmap mFiveBmap;    
    
    private int mNoneBmapWidth,mNoneBmapHeight;
    private int mOneBmapWidth,mOneBmapHeight;
    private int mTwoBmapWidth,mTwoBmapHeight;
    private int mThreeBmapWidth,mThreeBmapHeight;
    
    
    
    private int mBTN_MAX_Secect_state       = 0;

    
    public int mBTN_ResID_Background      = 0;
     
    
 
    /**
	 * @param context
	 */
	public MutilModeImageTextView(Context context) {
		super(context);
		Log.e(TAG, "MutilModeImageButton(Context context)");
		// TODO Auto-generated constructor stub
	}

	
	public MutilModeImageTextView(Context context,int res_id1,int res_id2,int res_id3,int res_background_id,int initstat)
	{
		super(context);
	    mBTN_ResID_Secect_None       = res_id1;
	    mBTN_ResID_Secect_One       = res_id2;
	    mBTN_ResID_Secect_Two       = res_id3;
	    mBTN_ResID_Background       = res_background_id;
	    mBTN_MAX_Secect_state =3;
	    mBTN_Secect_state = initstat;
//	    updatebackground(initstat);

	}

	public MutilModeImageTextView(Context context,int res_id1,int res_id2,int res_background_id,int initstat)
	{
		super(context);
	    mBTN_ResID_Secect_None       = res_id1;
	    mBTN_ResID_Secect_One       = res_id2;
	    
	    mBTN_ResID_Background       = res_background_id;
	    
        mNoneBmap =       BitmapFactory.decodeResource(getResources(), mBTN_ResID_Secect_None);
        mNoneBmapWidth = mNoneBmap.getWidth();
        mNoneBmapHeight = mNoneBmap.getHeight();

        mOneBmap =       BitmapFactory.decodeResource(getResources(), mBTN_ResID_Secect_One);
        mOneBmapWidth = mOneBmap.getWidth();
        mOneBmapHeight = mOneBmap.getHeight();

        mBTN_MAX_Secect_state =2;
	    mBTN_Secect_state = initstat;

//	    updatebackground(initstat);

	}	
	

	public MutilModeImageTextView(Context context,int res_id1,int res_background_id)
	{
		super(context);
	    mBTN_ResID_Secect_None       = res_id1;
	    
	    mBTN_ResID_Background       = res_background_id;
	    
        mNoneBmap =       BitmapFactory.decodeResource(getResources(), mBTN_ResID_Secect_None);
        mNoneBmapWidth = mNoneBmap.getWidth();
        mNoneBmapHeight = mNoneBmap.getHeight();

        mBTN_MAX_Secect_state =1;
	    mBTN_Secect_state = 0;


	}	
	
	public void SetOnlyOneIagme(int res_id1,int res_background_id)
	{
	    mBTN_ResID_Secect_None       = res_id1;
	    
	    mBTN_ResID_Background       = res_background_id;
	    
        mNoneBmap =       BitmapFactory.decodeResource(getResources(), mBTN_ResID_Secect_None);
        mNoneBmapWidth = mNoneBmap.getWidth();
        mNoneBmapHeight = mNoneBmap.getHeight();

        mBTN_MAX_Secect_state =1;
	    mBTN_Secect_state = 0;
		
	    this.setBackgroundDrawable(getResources().getDrawable(mBTN_ResID_Background));

//	    updatebackground(initstat);

	}
	
	
	
//	public void  SetFinalImage(int res_id1)
//	{
//        mBTN_MAX_Secect_state =1;
//	    mBTN_Secect_state = 0;
//        mNoneBmap =       BitmapFactory.decodeResource(getResources(), res_id1);
//       mNoneBmapWidth = mNoneBmap.getWidth();
//        mNoneBmapHeight = mNoneBmap.getHeight();
	    
//	}
	
	/**
	 * @param context
	 * @param attrs
	 */
	public MutilModeImageTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
		Log.e(TAG, "MutilModeImageButton attrs");
		ReadCustomAttr(context, attrs);
		
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param context
	 * @param attrs
	 * @param defStyle
	 */
	public MutilModeImageTextView(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
		Log.e(TAG, "MutilModeImageButton defStyle");
		
		ReadCustomAttr(context, attrs);
		// TODO Auto-generated constructor stub
	}


	void ReadCustomAttr(Context context, AttributeSet attrs)
	{
		
      	Log.d(TAG,"ReadCustomAttr(Context context, AttributeSet attrs)");	 	        
		
        TypedArray a = context.obtainStyledAttributes(attrs, 
        		R.styleable.mutilmodeimagebutton); 
        
        
         mBTN_ResID_Secect_None       = a.getResourceId(R.styleable.mutilmodeimagebutton_select_none_icon, 0);
         mBTN_ResID_Secect_One       = a.getResourceId(R.styleable.mutilmodeimagebutton_select_one_icon, 0);
         mBTN_ResID_Secect_Two       = a.getResourceId(R.styleable.mutilmodeimagebutton_select_two_icon, 0);
         mBTN_ResID_Secect_Three       = a.getResourceId(R.styleable.mutilmodeimagebutton_select_three_icon, 0);    
//         mBTN_ResID_Secect_Four      = a.getResourceId(R.styleable.mutilmodeimagebutton_select_four_icon, 0); 
//         mBTN_ResID_Secect_Five      = a.getResourceId(R.styleable.mutilmodeimagebutton_select_five_icon, 0); 
        
         mBTN_MAX_Secect_state       = a.getInteger(R.styleable.mutilmodeimagebutton_select_max_icons, 0); 
          
		
	}

	public int GetMutilModButtonState()
	{
		return mBTN_Secect_state;
	}

	public int GetMAXMutilModButtonState()
	{
		return mBTN_MAX_Secect_state;
	}	
	
    float x,y,touchStartX,touchStartY;
    
    @Override
	public boolean onTouchEvent(MotionEvent event)
	{

		boolean isMove = false;
		x = event.getRawX();
		y = event.getRawY() - 25;
//		Log.i("MyFloatView", "currentX=" + x + "--" + "currentY=" + y);

		switch (event.getAction())
		{

		case MotionEvent.ACTION_DOWN:
			touchStartX = event.getX();
			touchStartY = event.getY();
			auxiliaryFunction();
//			Log.i("startp", "touchStartX=" + touchStartX + "--"
//					+ "touchStartY=" + touchStartY);
			break;

		case MotionEvent.ACTION_MOVE:
			isMove = true;
			break;

			
		case MotionEvent.ACTION_UP:
			touchStartX = event.getX();
			touchStartY = event.getY();
//			updateViewPosition();

			break;
		}
        Log.i("isMove",""+isMove );
//		return isMove;
		return super.onTouchEvent(event);
	};	
	
//	@Override
//	public setText(CharSequence text)
//	{
//		 super.setText(text);
//	}

	public void updatebackground(int stat)
	{
		mBTN_Secect_state = stat;
     	int tmpresid=0;
     	if(mBTN_Secect_state>=mBTN_MAX_Secect_state)
     	{
     		mBTN_Secect_state =0;
     	}
     	switch(mBTN_Secect_state)
     	{
     	   case  BTN_SELECT_NONE:
     		   tmpresid = mBTN_ResID_Secect_None;
     		   break;
     		   
     	   case  BTN_SELECT_ONE:
     		   tmpresid = mBTN_ResID_Secect_One;
     		   break;
     		   
     	   case  BTN_SELECT_TWO:
     		   tmpresid = mBTN_ResID_Secect_Two;
     		   break;
     		   
     	   case  BTN_SELECT_THREE:
     		   tmpresid = mBTN_ResID_Secect_Three;
     		   break;
//     	   case  BTN_SELECT_FOUR:
//     		   tmpresid = mBTN_ResID_Secect_Four;
   		   
//     		   break;
//    	   case  BTN_SELECT_FIVE:
//     		   tmpresid = mBTN_ResID_Secect_Five;

//     		   break;
     		   
     	   default:
     		   break;
		   
     	}
//     	v.setBackground(getResources().getDrawable(tmpresid));
     
     	if(this.getText().length()==0)
     	{	
//    		Log.i("updatebackground", "mBTN_Secect_state=" + mBTN_Secect_state + "--" + "tmpresid=" + tmpresid);
     		 this.setBackgroundDrawable(getResources().getDrawable(mBTN_ResID_Background));
  //   	  this.setBackgroundResource(tmpresid);
     	}
    	else
     	{
    // 		this.setBackgroundResource(mBTN_ResID_Background);
    // 		String str =(String)this.getText();
    // 		this.setText(str);
     	}			
		
//     	this.setBackgroundDrawable(d);
	}
     
    protected void auxiliaryFunction() {
        if(mBTN_MAX_Secect_state>1)
     	mBTN_Secect_state=(mBTN_Secect_state+mBTN_MAX_Secect_state-1)%mBTN_MAX_Secect_state;
     	int tmpresid=0;
     	if(mBTN_Secect_state>=mBTN_MAX_Secect_state)
     	{
     		mBTN_Secect_state =0;
     	}
     	switch(mBTN_Secect_state)
     	{
     	   case  BTN_SELECT_NONE:
     		   tmpresid = mBTN_ResID_Secect_None;
     		   break;
     		   
     	   case  BTN_SELECT_ONE:
     		   tmpresid = mBTN_ResID_Secect_One;
     		   break;
     		   
     	   case  BTN_SELECT_TWO:
     		   tmpresid = mBTN_ResID_Secect_Two;
     		   break;
     		   
     	   case  BTN_SELECT_THREE:
     		   tmpresid = mBTN_ResID_Secect_Three;
     		   break;
//     	   case  BTN_SELECT_FOUR:
//     		   tmpresid = mBTN_ResID_Secect_Four;
   		   
//     		   break;
//     	   case  BTN_SELECT_FIVE:
//     		   tmpresid = mBTN_ResID_Secect_Five;

//     		   break;
     		   
     	   default:
     		   break;
		   
     	}
//     	v.setBackground(getResources().getDrawable(tmpresid));
     	if(this.getText().length()==0)
     	{	
     	  this.setBackgroundResource(mBTN_ResID_Background);
     	}
    	else
     	{
    // 		this.setBackgroundResource(mBTN_ResID_Background);
    // 		String str =(String)this.getText();
    // 		this.setText(str);
     	}	
 //    		this.setBackgroundDrawable(d)
    	
    }
	
    @SuppressLint("NewApi")
	@Override
    public void onDraw(Canvas c) {
//        mDrawable.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
//        mDrawable.draw(c);
    	Paint paint =new Paint();
    	int nleft=0,ntop=0,nHeight,nWidth=0;
    	nHeight = this.getHeight();
    	nWidth = this.getWidth();
    	super.onDraw(c);
//		Log.e(TAG, "onDraw(Context context)");
		if(this.getText().length()==0)
		{
			
            if(BTN_SELECT_NONE==mBTN_Secect_state)
            {
                if(mNoneBmap.getByteCount()>0)
                {	
                	if(nHeight>mNoneBmapHeight)    ntop = (nHeight-mNoneBmapHeight)/2;
                	if(nWidth>mNoneBmapWidth)  nleft  =(nWidth-mNoneBmapWidth)/2;
 //       			Log.e(TAG, ntop+"TTTTTT "+nn+"kjj=="+ny+"onDraw(Context context)"+nright);
                  	c.drawBitmap(mNoneBmap, nleft, ntop, paint);
                    
                }
            }
            else
            if(BTN_SELECT_ONE==mBTN_Secect_state)
            {
                if(mOneBmap.getByteCount()>0)
                {	
                	if(nHeight>mOneBmapHeight)    ntop = (nHeight-mOneBmapHeight)/2;
                	if(nWidth>mOneBmapWidth)  nleft  =(nWidth-mOneBmapWidth)/2;
 //       			Log.e(TAG, ntop+"TTTTTT "+nn+"kjj=="+ny+"onDraw(Context context)"+nright);
                  	c.drawBitmap(mOneBmap, nleft, ntop, paint);
                    
                }

            }
            else // 
            {
            	
            }	
		}	
    }

 
	
}
